﻿Imports System.Data.OleDb

Public Class SearchRecipient

    Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelButton.Click
        Me.Close()
    End Sub

    Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        Dim fn As Boolean = False
        Dim ln As Boolean = False
        Dim em As Boolean = False
        Dim SearchSQL As String = ""

        If FNameText.Text <> "" Then
            fn = True
        End If

        If LNameText.Text <> "" Then
            ln = True
        End If

        If EMailText.Text <> "" Then
            em = True
        End If

        'inserted first name, last name and email
        If fn = True And ln = True And em = True Then
            SearchSQL = "SELECT * FROM Recipients WHERE FirstName ='" & FNameText.Text & "'AND LastName ='" & LNameText.Text & "'AND e-mail ='" & EMailText.Text & "'"

            'inserted first name and last name 
        ElseIf fn = True And ln = True And em = False Then
            SearchSQL = "SELECT * FROM Recipients WHERE FirstName ='" & FNameText.Text & "'AND LastName ='" & LNameText.Text & "'"

            'inserted first name and email
        ElseIf fn = True And ln = False And em = True Then
            SearchSQL = "SELECT * FROM Recipients WHERE FirstName ='" & FNameText.Text & "'AND e-mail ='" & EMailText.Text & "'"

            'inserted last name and email
        ElseIf fn = False And ln = True And em = True Then
            SearchSQL = "SELECT * FROM Recipients WHERE LastName ='" & LNameText.Text & "'AND e-mail ='" & EMailText.Text & "'"

            'inserted first name only
        ElseIf fn = True And ln = False And em = False Then
            SearchSQL = "SELECT * FROM Recipients WHERE FirstName ='" & FNameText.Text & "'"

            'inserted last name only
        ElseIf fn = False And ln = True And em = False Then
            SearchSQL = "SELECT * FROM Recipients WHERE LastName ='" & LNameText.Text & "'"

            'inserted email only
        ElseIf fn = False And ln = False And em = True Then
            SearchSQL = "SELECT * FROM Recipients WHERE e-mail ='" & EMailText.Text & "'"
        End If

        Dim con As OleDbConnection = New OleDbConnection
        Dim Dadapter As OleDbDataAdapter
        Dim DSet As DataSet
        

        Try
            con.ConnectionString = My.Settings.OurDataBaseConnectionString
            con.Open()
            Dadapter = New OleDbDataAdapter(SearchSQL, con)
            DSet = New DataSet
            Dadapter.Fill(DSet, "Recipients")
            SearchResults.DataSource = DSet.Tables("Recipients")

            con.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click

        If Not SearchResults.CurrentRow.IsNewRow Then

            If SearchResults.SelectedRows.Count = 0 Then
                MsgBox("No row was selected. If you are trying to remove a row, highlight the entire row by clicking on the identifier column on the far left.", MessageBoxIcon.Error, "Entire Row Not Selected")

            ElseIf SearchResults.SelectedRows.Count = 1 Then
                Dim response As DialogResult = MessageBox.Show("Are you sure you want to delete the selected row?", "Delete row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
                If (response = DialogResult.Yes) Then
                    'SearchResults.Rows.Remove(SearchResults.SelectedRows(0))

                    Dim sqlstring As String = "Delete from Recipients where idR = " & SearchResults.SelectedRows(0).Cells(0).Value.ToString
                    Dim sqlstring2 As String = "Delete from Sent where idR = " & SearchResults.SelectedRows(0).Cells(0).Value.ToString
                    'MessageBox.Show(sqlstring)
                    Try
                        Using cnn As New OleDbConnection(My.Settings.OurDataBaseConnectionString)
                            Using cmd As New OleDbCommand(sqlstring2, cnn)
                                cnn.Open()
                                cmd.ExecuteNonQuery()
                                cnn.Close()
                            End Using
                            Using cmd As New OleDbCommand(sqlstring, cnn)
                                cnn.Open()
                                cmd.ExecuteNonQuery()
                                cnn.Close()
                            End Using
                            
                        End Using

                        Me.SearchResults.Rows.RemoveAt(Me.SearchResults.CurrentRow.Index)

                    Catch ex As Exception
                        MessageBox.Show(ex.Message)
                    End Try
                    
                End If
            ElseIf SearchResults.SelectedRows.Count > 1 Then
                MsgBox("Multiple rows are currently selected. The remove function can only delete one row at a time. Please select a single row and try again.", MessageBoxIcon.Error, "Single Row Not Selected")
                SearchResults.ClearSelection()
            End If
        End If
    End Sub

    Private Sub EditButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditButton.Click

    End Sub
End Class